package MyGui;

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

import Element.AbsElem;
import Element.Geom.AbsElemGeom;
import DistSys.*;

public class ElemInfoDlg extends AbsElemInfoDlg implements WindowListener {
    private boolean bBlockComboTempl = false;

    private JTabbedPane tabbedPane;
    private JPanel northPanel, centerPanel;
    private JLabel[]     lblsNorth;
    private JTextField[] tfsNorth, tfsFont;
    private JCheckBox[]  checksFont;
    private JCheckBox    checkShowName, checkShowDesc;
    private JList[]      lstsShowIndex;
    private JComboBox    comboTempl;

    private static final int[] GENERAL_INDEX = new int[] {
        /*
    	AbsElem.DATA_LENGTH,
        -1,  // separator
        AbsElem.DATA_ZONE, 
        AbsElem.DATA_LAYER,
        -1,  // separator
        AbsElem.DATA_CUSTOMERS,
        AbsElem.DATA_SRC_VOLT,
        AbsElem.DATA_CONST_POWER,
        AbsElem.DATA_PF_CONST_POWER,
        AbsElem.DATA_IS_LEADING,
        -1,  // separator
        AbsElem.DATA_RATED_POWER,
        AbsElem.DATA_RATED_CURR,
        -1,  // separator
        AbsElem.DATA_RECLOSING,
        AbsElem.DATA_INST_BLOCKED,*/
        // added by Yanli
        AbsElem.DATA_GEN_BUS, 
    	AbsElem.DATA_GEN_PG,
    	AbsElem.DATA_GEN_QG,
    	AbsElem.DATA_GEN_QMAX,
    	AbsElem.DATA_GEN_QMIN,
    	AbsElem.DATA_GEN_VG,
    	AbsElem.DATA_GEN_MBASE,
    	AbsElem.DATA_GEN_STATUS,
    	AbsElem.DATA_GEN_PMAX,
    	AbsElem.DATA_GEN_PMIN,
    	AbsElem.DATA_GEN_BID,
    };
    private static final int[] GENERAL2_INDEX = new int[] {
        AbsElem.RES_QUERY_SCORE_PERC
    };
    private static final int[] GENERAL3_INDEX = new int[] {
        AbsElem.DATA_ZONE_BUFFER0,
        AbsElem.DATA_ZONE_BUFFER1,
        AbsElem.DATA_ZONE_BUFFER2,
        AbsElem.DATA_ZONE_BUFFER3,
        AbsElem.DATA_ZONE_BUFFER4
    };
    
    /* Yanli
    private static final int[] REL_INDEX = new int[] {
        AbsElem.DATA_TEMP_FAULT,
        AbsElem.DATA_SUST_FAULT,
        AbsElem.DATA_OPEN_CKT,
        AbsElem.DATA_MTTR,
        AbsElem.DATA_MTTS,
        AbsElem.DATA_OPER_FAIL,
        -1,  // separator
        AbsElem.DATA_VOLT_SAG_LEVEL
    };
    private static final int[] REL2_INDEX = new int[] {
        AbsElem.RES_MOMENT_INT,
        AbsElem.RES_SUST_INT,
        AbsElem.RES_OUT_DUR,
        AbsElem.RES_VOLT_SAGS,
        -1,  // separator
        AbsElem.RES_SWT_OPER,
        AbsElem.RES_TRIP_OPER,
        AbsElem.RES_RECLOSE_OPER,
        AbsElem.RES_BLOCK_OPER,
        -1,  // separator
        AbsElem.RES_CAPA_CONS,
        AbsElem.RES_CAPA_CONS_KVA
    };

    
    private static final int[] PF_INDEX = new int[] {
        AbsElem.DATA_RESISTANCE,
        AbsElem.DATA_REACTANCE,
        AbsElem.DATA_SHUNT_COND,
        AbsElem.DATA_SHUNT_SUCEP,
        AbsElem.DATA_CONST_CURR,
        AbsElem.DATA_PF_CONST_CURR
    };
    private static final int[] PF2_INDEX = new int[] {
        AbsElem.RES_KVA_FLOW,
        AbsElem.RES_PF,
        -1,  // separator
        AbsElem.RES_VOLT_MAG,
        AbsElem.RES_VOLT_ANG,
        AbsElem.RES_CURR_MAG,
        AbsElem.RES_CURR_ANG,
        AbsElem.RES_SHORTCIRC_CURR,
        -1,  // separator
        AbsElem.RES_VOLT_DROP,
        AbsElem.RES_CURR_LOADING,
        AbsElem.RES_KVA_RATING
    };

    private static final int[] PQ_INDEX = new int[] {
        AbsElem.DATA_VOLT_SAG_LEVEL
    };
    private static final int[] PQ2_INDEX = new int[] {
        AbsElem.RES_VOLT_SAGS,
        -1,  // separator
        AbsElem.RES_SA_MOMENT_INT,
        AbsElem.RES_SA_SUST_INT,
        AbsElem.RES_SA_DUR_SI,
        AbsElem.RES_OLV_MI,
        AbsElem.RES_OLV_SI,
        AbsElem.RES_OLV_DUR_SI,
        AbsElem.RES_UVV_MI,
        AbsElem.RES_UVV_SI,
        AbsElem.RES_UVV_DUR_SI,
        -1,  // separator
        AbsElem.RES_PROTECT_ZONE
    };

    private static final int[] ECON_INDEX = new int[] {
        AbsElem.DATA_CAPITAL_COST,
        AbsElem.DATA_OM_COST,
        AbsElem.DATA_COST_PER_INT_KW,
        AbsElem.DATA_COST_PER_INT_KWH,
        AbsElem.DATA_COST_PER_SAG
    };
    private static final int[] ECON2_INDEX = new int[] {
        AbsElem.RES_CAPITAL_DELIV,
        AbsElem.RES_OM_DELIV,
        AbsElem.RES_LOAD_LOSS,
        AbsElem.RES_NO_LOAD_LOSS,
        AbsElem.RES_NET_DELIV_COST
    };

    private static final int[] VOLTVAR_INDEX = new int[] {
        AbsElem.DATA_TXFM_RATIO,
        AbsElem.DATA_TAP_RATIO,
        AbsElem.DATA_NUM_BOOST,
        AbsElem.DATA_NUM_BUCK,
        AbsElem.DATA_BOOST_TRIG,
        AbsElem.DATA_BUCK_TRIG
    };
    private static final int[] VOLTVAR2_INDEX = new int[] {
        AbsElem.RES_TAP_POS
    };

    private static final int[] MISC_INDEX = new int[] {
        AbsElem.DATA_PHASE,
        -1,  // separator
        AbsElem.DATA_OPTIMIZABLE,
        AbsElem.DATA_IN_OPT_LIB,
        -1,  // separator
        AbsElem.DATA_MAINT_RATE,
        AbsElem.DATA_MAINT_DUR
    };
    private static final int[] MISC2_INDEX = new int[] {
        AbsElem.RES_VOLT_SAGS_ON_TGT
    };
*/
    private static final int[] FONT_INDEX = new int[] {
        AbsElem.DATA_FONT_SIZE
    };

    public ElemInfoDlg(MainFrame parent, String title, boolean modal,
                       AbsElem elem) {
        super(parent, title, modal, elem);

        font = GuiConst.fontDefault;

        northPanel = createPanelNorth();
        centerPanel = createPanelCenter();
        JPanel southPanel = createPanelSouth();

        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.add(northPanel, BorderLayout.NORTH);
        contentPane.add(centerPanel, BorderLayout.CENTER);
        contentPane.add(southPanel, BorderLayout.SOUTH);

        this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
        this.addWindowListener(this);
        this.setLocation(200, 200);
        this.setSize(510, 515);
        //this.setResizable(false);
        this.setVisible(true);
    }

    protected JPanel createDataPanel(int[] indices) {
        return createPanel(indices, " Data ",
            PANEL_DATA | PANEL_CHECK | PANEL_EDIT);
    }

    protected JPanel createResultPanel(int[] indices) {
        return createPanel(indices, " Results ", 0);
    }

    /*========================================
    Create three major panels:
        createNorthPanel()
        createCenterPanel()
        createSouthPanel()
    ========================================*/
    private JPanel createPanelNorth(){
        JPanel p = new JPanel();
        p.setLayout(MyDlgFrame.gridBagLayout);

        // north panel
        lblsNorth = new JLabel[2];
        lblsNorth[0] = new JLabel(); lblsNorth[0].setForeground(Color.blue);
        lblsNorth[1] = new JLabel(); lblsNorth[1].setForeground(Color.blue);
        tfsNorth = new JTextField[2];
        tfsNorth[0] = new JTextField("",15);
        tfsNorth[0].addFocusListener(focusListener);
        tfsNorth[0].setInputVerifier(new InputVerifier() {
            public boolean verify(JComponent input) {
                bModified = true;
                elem.getElemGeomObj().setName(((JTextField)input).getText());
                return true;
            }
        });
        tfsNorth[1] = new JTextField("",15);
        tfsNorth[1].addFocusListener(focusListener);
        tfsNorth[1].setInputVerifier(new InputVerifier() {
            public boolean verify(JComponent input) {
                bModified = true;
                elem.getElemGeomObj().setDesc(((JTextField)input).getText());
                return true;
            }
        });
        comboTempl = new JComboBox();
        comboTempl.addActionListener(alComboTempl);

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3, 3, 3, 3);
        c.fill = GridBagConstraints.BOTH;

        c.gridx = 0; p.add(new JLabel("Name:"), c);
        c.gridx = 1; c.weightx = 1; p.add(tfsNorth[0], c);
        c.gridx = 2; c.weightx = 0; p.add(new JLabel("ID:"), c);
        c.gridx = 3; p.add(lblsNorth[0], c);

        c.gridy = 1;
        c.gridx = 0; p.add(new JLabel("Description: "), c);
        c.gridx = 1; c.weightx = 1; p.add(tfsNorth[1], c);
        c.gridx = 2; c.weightx = 0; p.add(new JLabel("Type:"), c);
        c.gridx = 3; p.add(lblsNorth[1], c);

        c.insets.bottom = 10;
        c.gridy = 2;
        c.gridx = 0; p.add(new JLabel("Default Template: "), c);
        c.gridx = 1; c.weightx = 1; p.add(comboTempl, c);
        c.gridx = 3; c.weightx = 0;
        // Dummy label to ensure width of the type field even for Dist. Gen.
        p.add(new JLabel("                                             "), c);

        return p;
    }

    private JPanel createPanelCenter(){
        JPanel p = new JPanel();
        tabbedPane = new JTabbedPane();
        tabbedPane.addTab("General", createTabGeneral());
        /* Yanli
        tabbedPane.addTab("Reliability",
            createTab(createDataPanel(REL_INDEX), createResultPanel(REL2_INDEX)));
        
        tabbedPane.addTab("Power Flow",
            createTab(createDataPanel(PF_INDEX), createResultPanel(PF2_INDEX)));
        tabbedPane.addTab("Power Quality",
            createTab(createDataPanel(PQ_INDEX), createResultPanel(PQ2_INDEX)));
        tabbedPane.addTab("Economics",
            createTab(createDataPanel(ECON_INDEX), createResultPanel(ECON2_INDEX)));
        tabbedPane.addTab("Volt/Var",
            createTab(createDataPanel(VOLTVAR_INDEX), createResultPanel(VOLTVAR2_INDEX)));
        if (SysConst.DEBUG_MODE > 0)
            tabbedPane.addTab("Misc",
                createTab(createDataPanel(MISC_INDEX), createResultPanel(MISC2_INDEX)));
        tabbedPane.addTab("Text", createTabText());
		*/
        //Add the tabbed pane to this panel.
        p.setLayout(new GridLayout(1, 1, 2, 2));
        p.add(tabbedPane);
        return p;
    }

    private JPanel createPanelSouth(){
        JPanel p = new JPanel();

        class UnselectableCheckBox extends JCheckBox implements ActionListener {
            private boolean bSel;
            public UnselectableCheckBox(String text, boolean selected) {
                super(text);
                setSelected(bSel = selected);
                addActionListener(this);
            }
            public void actionPerformed(ActionEvent evt) { setSelected(bSel); }
            public boolean isFocusTraversable() { return false; }
        };

        p.add(new JLabel("Check box legend: "));
        p.add(new UnselectableCheckBox("Default value", false));
        p.add(new UnselectableCheckBox("Customized value", true));
        JCheckBox checkAuto = new UnselectableCheckBox("", false);
        checkAuto.setBorderPainted(true);
        p.add(checkAuto);
        p.add(new JLabel("Auto value"));
        
        return p;
    }

    /*========================================
    Create the tabs:
        createTab()
        createTabGeneral()
        createTabText()
    ========================================*/
    
	private JPanel createTab(JPanel left, JPanel right) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(GAP, GAP, GAP, GAP);
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = c.weighty = 1;

        JPanel wrapper = new JPanel(MyDlgFrame.gridBagLayout);
        wrapper.add(left, c);
        panel.add(wrapper);

        wrapper = new JPanel(MyDlgFrame.gridBagLayout);
        wrapper.add(right, c);
        panel.add(wrapper);

        return panel;
    }

    private JPanel createTabGeneral() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1, 2));

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(GAP, GAP, GAP, GAP);
        c.anchor = GridBagConstraints.NORTH;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = c.weighty = 1;

        JPanel wrapper = new JPanel(MyDlgFrame.gridBagLayout);
        wrapper.add(createDataPanel(GENERAL_INDEX), c);
        panel.add(wrapper);

        wrapper = new JPanel(MyDlgFrame.gridBagLayout);
        // NOTE: Uncomment the following lines to add the "Zone Buffers" box.
        //
        // c.weighty = 0;
        wrapper.add(createResultPanel(GENERAL2_INDEX), c);
        // c.gridy = 1; c.weighty = 1;
        // wrapper.add(createPanel(GENERAL3_INDEX, " Zone Buffers ",
        //     PANEL_DATA),c);
        panel.add(wrapper);

        return panel;
    }

    /* Yanli
    private JPanel createTabText() {
        JPanel panel = new JPanel(MyDlgFrame.gridBagLayout);

        JPanel top = new JPanel(MyDlgFrame.gridBagLayout);
        JPanel left = new JPanel(MyDlgFrame.gridBagLayout);
        JPanel right = new JPanel(MyDlgFrame.gridBagLayout);

        // tab text top
        tfsFont = new JTextField[] { new JTextField(6) };
        tfsFont[0].setHorizontalAlignment(JTextField.LEFT);
        checksFont = new JCheckBox[] { new JCheckBox(
            SysConst.getDataFieldFromValueIndex(AbsElem.DATA_FONT_SIZE))
        };
        tfsFont[0].addFocusListener(focusListener);
        tfsFont[0].setInputVerifier(new DataSaveVerifier(tfsFont, FONT_INDEX));
        checksFont[0].addActionListener(new CheckSaveActionListener(
            checksFont, tfsFont, FONT_INDEX));
        // ActionListener for ShowName and ShowDesc
        ActionListener listener = new ActionListener() {
            public void actionPerformed(ActionEvent evt){
                bModified = true;
                if (evt.getSource() == checkShowName)
                    elem.getElemGeomObj().setShowName(checkShowName.isSelected());
                else
                    elem.getElemGeomObj().setShowDesc(checkShowDesc.isSelected());

            }
        };
        checkShowName = new JCheckBox("Show Name");
        checkShowName.addActionListener(listener);
        checkShowDesc = new JCheckBox("Show Description");
        checkShowDesc.addActionListener(listener);

        // tab text bottom
        InputVerifier verifier = new InputVerifier() {
            public boolean verify(JComponent input) {
                AbsElemGeom geom = elem.getElemGeomObj();
                int iCat = 0;
                while (input != lstsShowIndex[iCat])
                    iCat++;

                int[] aiSel = ((JList)input).getSelectedIndices();
                if (aiSel.length == 0 || aiSel[0] == 0) {
                    // nothing or [None] is selected
                    if (geom.getShowIndex(iCat)) {
                        bModified = true;
                        geom.setShowIndex(iCat, null);
                    }
                } else {
                    // map indices
                    if (iCat == AbsElemGeom.SHOW_DATA) {
                        for (int i = 0; i < aiSel.length; i++) {
                            aiSel[i] = SysConst.
                                mapDataIndexFromDisplayToValue(aiSel[i]);
                        }
                    } else { // (iCat == AbsElemGeom.SHOW_RESULT)
                        for (int i = 0; i < aiSel.length; i++) {
                            aiSel[i] = SysConst.
                                mapResultIndexFromDisplayToValue(aiSel[i]);
                        }
                    }
                    // check for modification
                    boolean bLocalModified = !geom.getShowIndex(iCat) ||
                        (aiSel.length != geom.getShowIndexCount(iCat));
                    for (int i = 0; !bLocalModified && i < aiSel.length; i++) {
                        if (aiSel[i] != geom.getShowIndex(iCat, i))
                            bLocalModified = true;
                    }
                    if (bLocalModified) {
                        // set new indices
                        bModified = true;
                        geom.setShowIndex(iCat, aiSel);
                    }
                }
                return true;
            }
        };
        lstsShowIndex = new JList[AbsElemGeom.NUM_SHOW];
        lstsShowIndex[AbsElemGeom.SHOW_DATA] = new JList(SysConst.ssDataField);
        lstsShowIndex[AbsElemGeom.SHOW_RESULT]=new JList(SysConst.ssResultField);
        for (int iCat = 0; iCat < AbsElemGeom.NUM_SHOW; iCat++) {
            lstsShowIndex[iCat].setSelectionMode(
                ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
            lstsShowIndex[iCat].addFocusListener(focusListener);
            lstsShowIndex[iCat].setInputVerifier(verifier);
        }

        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(GAP, GAP, GAP, GAP);
        c.anchor = GridBagConstraints.NORTHWEST;
        c.fill = GridBagConstraints.BOTH;
        c.gridwidth = 2; c.weightx = 1; panel.add(top, c);
        c.gridwidth = 1; c.gridy = 1; c.weighty = 1; panel.add(left, c);
        c.gridx = 1; panel.add(right, c);

        top.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), " General "));
        Box wrapper = new Box(BoxLayout.X_AXIS);
        wrapper.add(checksFont[0]);
        wrapper.add(tfsFont[0]);
        c.fill = GridBagConstraints.NONE;
        c.insets.top = 0; c.insets.bottom = 0;
        c.gridx = 0; c.gridy = 0; c.weighty = 0; top.add(wrapper, c);
        c.gridy = 1; top.add(checkShowName, c);
        c.gridy = 2; top.add(checkShowDesc, c);

        left.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), " Data Fields to Display "));
        right.setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createEtchedBorder(), " Results Fields to Display "));
        c.fill = GridBagConstraints.BOTH;
        c.gridy = 0; c.weighty = 1; c.insets.bottom = GAP;
        left.add(new JScrollPane(lstsShowIndex[AbsElemGeom.SHOW_DATA]), c);
        right.add(new JScrollPane(lstsShowIndex[AbsElemGeom.SHOW_RESULT]), c);

        return panel;
    }
*/
    /*======================================
    Implement WindowListener interfaces
    ======================================*/
    public void windowOpened(WindowEvent evt) { }
    public void windowClosed(WindowEvent evt) { }
    public void windowIconified(WindowEvent evt) { }
    public void windowDeiconified(WindowEvent evt) { }
    public void windowActivated(WindowEvent evt) {
        recreateAllPanels();
        updatePanelNorth();
        updateTabAll();
    }
    public void windowDeactivated(WindowEvent evt) {
     if (verifyFocus()) {
    	   if (bModified) {
    		   
                DrawingPanel drawingPanel = mainFrame.mainPanel; //((MainFrame)getParent()).mainPanel;
                drawingPanel.setNeedRedraw(true);
                drawingPanel.repaint();
    		   
            }
        }
        comboTempl.hidePopup();
    }
    public void windowClosing(WindowEvent evt) {
        if (verifyFocus()) {
            if (bModified) {
                DrawingPanel drawingPanel = mainFrame.mainPanel;
                drawingPanel.setNeedRedraw(true);
                drawingPanel.repaint();
            }
        	
            comboTempl.hidePopup();
            hide();
        }
    }

    /*==============================================
      updates
    ==============================================*/
    private void updatePanelNorth(){
        AbsElemGeom geom = elem.getElemGeomObj();
        AbsElem dftTemp = elem.getDftTemp();

        lblsNorth[0].setText("" + (Sys.elemList.indexOf(elem) + 1));
                                     // User's version = developer's version + 1
        lblsNorth[1].setText(elem.getStringForType());

        tfsNorth[0].setText(geom.getName());
        tfsNorth[1].setText(geom.getDesc());

        bBlockComboTempl = true;
        DefaultComboBoxModel model = (DefaultComboBoxModel)comboTempl.getModel();
        model.removeAllElements();
        int iSubList = SysConst.mapTypeToTemplType(elem.getType());
        if (iSubList >= 0) {
            // Add nameList if active template was found.
            int sz = Templates.nameLists[iSubList].size();
            for(int i = 0; i < sz; i++){
                model.addElement(Templates.nameLists[iSubList].elementAt(i));
            }
            if (dftTemp != null) {
                int index =
                    Templates.subLists[iSubList].indexOf(dftTemp);
                if (index >= 0) {
                    // Select current template if it is in the list, ...
                    comboTempl.setSelectedIndex(index);
                } else {
                    // ... add and select current template if not.
                    String sName = dftTemp.getElemGeomObj().getName();
                    model.addElement(sName);
                    comboTempl.setSelectedItem(sName);
                }
            } else {
                // Select nothing if no current template exists.
                comboTempl.setSelectedIndex(-1);
            }
        } else if (dftTemp != null) {
            // If active template was not found (BTW how could that happen?) but
            // current template exists, add and select it.
            String sName = dftTemp.getElemGeomObj().getName();
            model.addElement(sName);
            comboTempl.setSelectedItem(sName);
        }
        bBlockComboTempl = false;
    }

    private void updateTabAll(){
        updateAllPanels();

        // update tab Text
        AbsElemGeom geom = elem.getElemGeomObj();

        boolean bFlag = elem.getCustomizedFlag(AbsElem.DATA_FONT_SIZE);
        checksFont[0].setSelected(bFlag);
        tfsFont[0].setEnabled(bFlag);
        tfsFont[0].setText(nf.format(elem.getData(AbsElem.DATA_FONT_SIZE)));

        checkShowName.setSelected(geom.getShowName());
        checkShowDesc.setSelected(geom.getShowDesc());

        for (int iCat = 0; iCat < AbsElemGeom.NUM_SHOW; iCat++) {
            lstsShowIndex[iCat].clearSelection();
            if (geom.getShowIndex(iCat)) {
                int count = geom.getShowIndexCount(iCat);
                for(int i = 0; i < count; i++) {
                    int index = geom.getShowIndex(iCat, i);
                    int iRow;
                    if (iCat == AbsElemGeom.SHOW_DATA)
                        iRow = SysConst.mapDataIndexFromValueToDisplay(index);
                    else // (iCat == AbsElemGeom.SHOW_RESULT)
                        iRow = SysConst.mapResultIndexFromValueToDisplay(index);
                    lstsShowIndex[iCat].addSelectionInterval(iRow, iRow);
                }
            } else
                lstsShowIndex[iCat].setSelectedIndex(0);
        }
    }

    /*=============================================
    Implement ActionListener
    =============================================*/
    private ActionListener alComboTempl = new ActionListener(){
        public void actionPerformed(ActionEvent evt){
            if (bBlockComboTempl)
                return; // skip if this is for a new elem

            String templName = (String)comboTempl.getSelectedItem();
            AbsElem dftElem = Templates.mapStringToObject(templName);
            elem.setDftTemp(dftElem);
            if (dftElem != null) {
                Templates.copyAllDataFromTemp(elem, dftElem);
            } else {
                Object obj = evt.getSource();
                Component cmp = null;
                if( obj instanceof Component ) { cmp = (Component) obj; }
                JOptionPane.showMessageDialog( cmp, "No Template found.");
            }
            updateTabAll();
            bModified = true;
        }//end of actionPerformed(): alComboTempl
    };//end of alComboTempl
}